package com.eqdd.yiqidian.utils;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.eqdd.yiqidian.ListItem92Custom;
import com.eqdd.yiqidian.ListItem97Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author吕志豪 .
 * @date 17-12-26  上午10:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class BottomSheetUtil {
    public static BottomSheetDialog getBottomSheet(Activity activity, String title, ItemClickSupport.OnItemClickListener itemClick, String... items) {
        ArrayList<ItemBean> ItemBeans = new ArrayList<>();
        for (String item : items) {
            ItemBeans.add(new ItemBean(item));
        }
        return getBottomSheet(activity, title, itemClick, ItemBeans);
    }

    public static BottomSheetDialog getBottomSheet(Activity activity, String title, ItemClickSupport.OnItemClickListener itemClick, ArrayList<ItemBean> ItemBeans) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        ListItem92Custom inflate = DataBindingUtil.inflate(activity.getLayoutInflater(), R.layout.list_item_92, null, false);
        if (TextUtils.isEmpty(title)) {
            inflate.tvTitle.setVisibility(View.GONE);
        } else {
            inflate.tvTitle.setVisibility(View.VISIBLE);
            inflate.tvTitle.setText(title);
        }
        inflate.tvCancel.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
        });
        inflate.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        inflate.recyclerView.addItemDecoration(new DividerLineItemDecoration(activity).setLastDraw(false));
        SlimAdapterEx slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_93, new SlimInjector<ItemBean>() {
            @Override
            public void onInject(ItemBean data, IViewInjector injector) {
                injector.text(R.id.tv_content, data.content)
                        .textColor(R.id.tv_content, data.color);
            }
        }).attachTo(inflate.recyclerView).updateData(ItemBeans);
        ItemClickSupport.addTo(inflate.recyclerView)
                .setOnItemClickListener(itemClick);
        bottomSheetDialog.setContentView(inflate.getRoot());
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;
    }

    public static TimeWrapper getTimePicker(Activity activity, String title, DateMake dateMake) {
        final String[] date = new String[1];
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        ListItem97Custom inflate = DataBindingUtil.inflate(activity.getLayoutInflater(), R.layout.list_item_97, null, false);
        if (TextUtils.isEmpty(title)) {
            inflate.tvTitle.setVisibility(View.GONE);
        } else {
            inflate.tvTitle.setVisibility(View.VISIBLE);
            inflate.tvTitle.setText(title);
        }
        ClickUtil.click(inflate.tvMake, () -> {
            bottomSheetDialog.dismiss();
            if (TextUtils.isEmpty(date[0])) {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                date[0] = sf.format(new Date(inflate.calendarView.getDate()));
            }
            dateMake.make(date[0], inflate.rangeTimeSeekBar.getStartTime(), inflate.rangeTimeSeekBar.getEndTime());
        });
        inflate.calendarView.setOnDateChangeListener((calendarView, i, i1, i2) -> {
            java.util.Calendar instance = java.util.Calendar.getInstance();
            instance.set(i, i1, i2);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            date[0] = sf.format(instance.getTime());
            System.out.println(i + " " + i1 + " " + i2);
        });
        bottomSheetDialog.setContentView(inflate.getRoot());
        View parent = (View) inflate.getRoot().getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        inflate.getRoot().measure(0, 0);
        behavior.setPeekHeight(inflate.getRoot().getMeasuredHeight());
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return new TimeWrapper().setInflate(inflate).setBottomSheetDialog(bottomSheetDialog);
    }

    static class ItemBean {
        public String content;
        public int color = 0xff000000;
        public boolean isRedDot = false;

        public ItemBean(String content) {
            this.content = content;
        }
    }

    public interface ItemClick {
        void click(int pos);
    }

    public interface DateMake {
        void make(String date, String fromTime, String toTime);
    }

    public static class TimeWrapper {
        ListItem97Custom inflate;
        BottomSheetDialog bottomSheetDialog;

        public TimeWrapper setInflate(ListItem97Custom inflate) {
            this.inflate = inflate;
            return this;
        }

        public TimeWrapper setBottomSheetDialog(BottomSheetDialog bottomSheetDialog) {
            this.bottomSheetDialog = bottomSheetDialog;
            return this;
        }

        public void updateTime(long date, String from, String to) {
            try {
                inflate.calendarView.setDate(date);
                SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
                java.util.Calendar startDate = java.util.Calendar.getInstance();
                startDate.setTime(sf.parse(from));
                java.util.Calendar endDate = java.util.Calendar.getInstance();
                endDate.setTime(sf.parse(to));
                inflate.rangeTimeSeekBar.setStartTime(startDate.get(java.util.Calendar.HOUR_OF_DAY), startDate.get(java.util.Calendar.MINUTE) / 10);
                inflate.rangeTimeSeekBar.setEndTime(endDate.get(java.util.Calendar.HOUR_OF_DAY), endDate.get(java.util.Calendar.MINUTE) / 10);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        public void show() {
            bottomSheetDialog.show();
        }
    }

}
